package com.daylightrobbery.service.impl;

import com.daylightrobbery.entity.Account;
import com.daylightrobbery.entity.UserSignInLog;
import com.daylightrobbery.mapper.UserSignInLogMapper;
import com.daylightrobbery.server.entity.DeviceInfo;
import com.daylightrobbery.server.entity.IpDetails;
import com.daylightrobbery.service.UserSignInLogService;
import com.daylightrobbery.utils.LogUtils;
import com.daylightrobbery.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserSignInLogServiceImpl implements UserSignInLogService {

    @Autowired
    private UserSignInLogMapper logMapper;



    @Override
    public void addLog(Account account, String ip, DeviceInfo deviceInfo, String type) {
        UserSignInLog userSignInLog = new UserSignInLog();
        userSignInLog.setUid(account.getUid())
                .setCtime(TimeUtils.getCurrentTime())
                .setDeviceName(deviceInfo.getDeviceName())
                .setDeviceType(deviceInfo.getDeviceType())
                .setDeviceModel(deviceInfo.getDeviceModel());
        IpDetails ipDetails = LogUtils.getIpDetails(ip);
        userSignInLog.setProvince(ipDetails.getPro())
                .setCity(ipDetails.getCity())
                .setAddr(ipDetails.getAddr())
                .setRegion(ipDetails.getRegion())
                .setIp(ip);
        userSignInLog.setType(type);
        addLog(userSignInLog);
    }

    public void addLog(UserSignInLog log) {
        logMapper.insertLog(log);
    }

    public UserSignInLog getLogById(int logId) {
        return logMapper.selectLogById(logId);
    }

    public void updateLog(UserSignInLog log) {
        logMapper.updateLog(log);
    }

    public void deleteLog(int logId) {
        logMapper.deleteLogById(logId);
    }

    public List<UserSignInLog> getAllLogs() {
        return logMapper.selectAllLogs();
    }
}
